package com.bbcare.followup.plat.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.*;
import com.bbcare.followup.plat.entity.FuTemplateRecord;
import com.bbcare.followup.plat.service.FuTemplateRecordService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title		FuTemplateRecordController.java
 * @Package		com.bbcare.followup.plat.controller
 * @Description	模板内容控制器
 * @Author		<a href="mailto:jiangqian_java@163.com">jiang</a>
 * @Date		2023/10/18
 * @Version		V1.0 
 * @Copyright	(c) Shanghai BBcare Medical Equipment Co.,Ltd.
 */
@RestController
@RequestMapping(value = "/fuTemplateRecord")
public class FuTemplateRecordController {

    @Resource
    private FuTemplateRecordService fuTemplateRecordService;

    /**
     * 新增
     * @author beichen
     * @date 2023/10/18
     **/
    @RequestMapping("/insert")
    public ResultModel insert(@RequestBody String arg, HttpServletRequest request){
        JSONObject jsonArg;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式！"));
        }

        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            // 前台必须传data节点，否则报错返回
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
        }
        HashMap<String, Object> paramMap = new HashMap<String, Object>();
        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String authorId = (String) sessionMap.get("authorId");
        String authorName = (String) sessionMap.get("authorName");// 登录账号医生姓名
        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        paramMap.put("authorName", authorName);

        FuTemplateRecord fuTemplateRecord = JSONObject.parseObject(jsonData.toJSONString(), FuTemplateRecord.class);
        fuTemplateRecord.setCreateTime(new Date());
        fuTemplateRecord.setCreateBy(authorId);
        return new ResultModel(fuTemplateRecordService.insert(fuTemplateRecord));
    }

    /**
     * 刪除
     * @author beichen
     * @date 2023/10/18
     **/
    @PostMapping("/delete/{id}")
    public ResultModel delete(@PathVariable("id") int id) {
        return new ResultModel(fuTemplateRecordService.delete(id));
    }

    /**
     * 更新
     * @author beichen
     * @date 2023/10/18
     **/
    @RequestMapping("/update")
    public ResultModel update(@RequestBody String arg, HttpServletRequest request){
        JSONObject jsonArg;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式！"));
        }

        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            // 前台必须传data节点，否则报错返回
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
        }
        HashMap<String, Object> paramMap = new HashMap<String, Object>();
        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String authorId = (String) sessionMap.get("authorId");
        String authorName = (String) sessionMap.get("authorName");// 登录账号医生姓名
        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        paramMap.put("authorName", authorName);

        FuTemplateRecord fuTemplateRecord = JSONObject.parseObject(jsonData.toJSONString(), FuTemplateRecord.class);
        fuTemplateRecord.setUpdateTime(new Date());
        fuTemplateRecord.setUpdateBy(authorId);
        return new ResultModel(fuTemplateRecordService.update(fuTemplateRecord));
    }

    /**
     * 查询 根据主键 id 查询
     * @author beichen
     * @date 2023/10/18
     **/
    @RequestMapping("/load/{id}")
    public ResultModel load(@PathVariable("id") int id){
        return new ResultModel(fuTemplateRecordService.load(id));
    }

    /**
     * 查询 分页查询
     * @author beichen
     * @date 2023/10/18
     **/
    @RequestMapping("/pageList")
    public Map<String, Object> pageList(@RequestBody String arg , HttpServletRequest httpServletRequest) {
        JSONObject jsonArg = null;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式！"));
        }

        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            // 前台必须传data节点，否则报错返回
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
        }
        String queueId = jsonData.getString("followQueueId");
        if (StringUtils.isBlank(queueId)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入队列id或值为空！"));
        }

        return fuTemplateRecordService.getList(jsonData);
    }

}
